<?php
class Line_destination_model extends CI_Model
{
    private $table = 'line_destination';
    private $table_2 = 'region';
    private $table_3 = 'line_related_destination';
    private $table_4 = 'line_base';
    
    public function find()
    {
        return $this->db->get($this->table);
    }

    public function total($search_param=array())
    {
        if (!empty($search_param['address'])) {
            $this->db->like('address', $search_param['address']);
        }
        if (!empty($search_param['country_id'])) {
            $this->db->where('country_id', $search_param['country_id']);
        }
        if (!empty($search_param['province_id'])) {
            $this->db->where('province_id', $search_param['province_id']);
        }
        if (!empty($search_param['city_id'])) {
            $this->db->where('city_id', $search_param['city_id']);
        }
        if (!empty($search_param['district_id'])) {
            $this->db->where('district_id', $search_param['district_id']);
        }
        return $this->db->count_all_results($this->table);
    }
    
    public function page_list($page_num, $num, $search_param=array())
    {
        if (!empty($search_param['address'])) {
            $this->db->like('address', $search_param['address']);
        }
        if (!empty($search_param['country_id'])) {
            $this->db->where('country_id', $search_param['country_id']);
        }
        if (!empty($search_param['province_id'])) {
            $this->db->where('province_id', $search_param['province_id']);
        }
        if (!empty($search_param['city_id'])) {
            $this->db->where('city_id', $search_param['city_id']);
        }
        if (!empty($search_param['district_id'])) {
            $this->db->where('district_id', $search_param['district_id']);
        }
        $this->db->order_by('destination_id','asc');
        $this->db->limit($page_num, $num);
        return $this->db->get($this->table);
    }

    /**  provider 供应商***/
    
    /**
     * 线路首页展示数据
     * @param array $line_travel_type 线路类型id
     * @param int $limit
     * @return resource
     */
    public function findByLinebase($data,$limit)
    {
        $this->db->distinct();
        $this->db->select('line_base.*');
        $this->db->from($this->table.' as line_destination');
        $this->db->join($this->table_3.' as line_related_destination', 'line_related_destination.destination_id = line_destination.destination_id');
        $this->db->join($this->table_4.' as line_base', 'line_related_destination.line_id = line_base.id');
    
        if (isset($data['line_travel_type']) && $data['line_travel_type']) {
        	$this->db->where('line_base.line_travel_type', $data['line_travel_type']);
        }
        
        if(isset($data['country_id']) && $data['country_id']){
            $this->db->where('line_destination.country_id', $data['country_id']);
        }
    
        if(isset($data['province_id']) && $data['province_id']){
            $this->db->where('line_destination.province_id', $data['province_id']);
        }
    
        if(isset($data['city_id']) && $data['city_id']){
            $this->db->where('line_destination.city_id', $data['city_id']);
        }
    
        if(isset($data['district_id']) && $data['district_id']){
            $this->db->where('line_destination.district_id', $data['district_id']);
        }
         
        $this->db->where('line_base.status', 1); //上架产品
        $this->db->where('line_base.audit_status', 1); //上架产品
        $this->db->order_by('line_base.homepage','ASC');
        $this->db->limit($limit);
        return $this->db->get();
    }
}